/**
 * 自定义
 * */


var formList = {
    title: '',
    imgList: [],
    tmp_code: '',
    thingList: [],
};


//获取数据
function getOne(){
    if (code == '' || code == undefined) {
        msgPop({msg: '码值错误'});
        return
    }
    var userInfo = getUserInfo();
    if (!token && userInfo) {
        token = userInfo.token;
    }
    var formData = {
        token: token,
        code: code
    };
    console.log(formData);
    $.ajax({
        type: apis.tmp2One.method,
        url: apis.tmp2One.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            formList = res.data;
            getHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//场景拼接字符串
function getHtml(){
    var h = '', i;
    //箱体外观
    h += '<style type="text/css">.boxTitle {color: #888; background: #eee;} #boxOutImgs {width: 100%; height: 200px; overflow: hidden;} #boxOutImgs img{width: 100%;} #boxOutImgBtn {padding: 5px 0; text-align: center; color: #0088CC;} #advert {width: 100%; height: 100px; overflow: hidden;} #advert img {width: 100%;}</style>';
    //箱体直观图
    h += '<table>';
    h += '<tr><td colspan="2" class="boxTitle">箱体外观</td></tr>';
    h += '<tr>';
    h += '<td colspan="2">';
    h += '<div id="boxOutImgs">';
    //for (i = 0; i < formList.imgList.length; i ++) {
    //    h += '<img src="' + formList.imgList[i].link + '"/>';
    //}
    if (formList.thumbList.length > 0) {
        h += '<img src="' + formList.thumbList[0].link + '"/>';
    }
    h += '</div>';
    h += '<div id="boxOutImgBtn" onclick="thumbShow();">查看大图</div>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //箱体信息
    h += '<table>';
    h += '<tr><td colspan="2" class="boxTitle">箱体信息</td></tr>';
    h += '<tr>';
    h += '<td class="tl">收纳名称</td>';
    h += '<td class="tr">';
    h += formList.title;
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">收纳编号</td>';
    h += '<td class="tr">';
    h += formList.tmp_code;
    h += '</td>';
    h += '</tr>';
    //h += '<tr>';
    //h += '<td class="tl">码编号</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    if (formList.cate_id > 0) {
        h += '<tr>';
        h += '<td class="tl">收纳类型</td>';
        h += '<td class="tr">';
        h += formList.cate_name
        h += '</td>';
        h += '</tr>';
    }
    h += '<tr>';
    h += '<td class="tl">箱体长</td>';
    h += '<td class="tr">';
    h += formList.size_c + ' mm';
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">箱体宽</td>';
    h += '<td class="tr">';
    h += formList.size_k + ' mm';
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">箱体高</td>';
    h += '<td class="tr">';
    h += formList.size_g + ' mm';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //点赞+评论
    h += '<style type="text/css">.td-hd { text-align: center; background: #00CC99; color: #eee; }</style>';
    h += '<table style="margin: 10px 0;">';
    h += '<tr>';
    h += '<td class="td-hd" onclick="addLike();">';
    if (formList.like_uid == 0) {
        h += '<img src="/imgs/xin0.png" height="20"/>';
    } else {
        h += '<img src="/imgs/xin1.png" height="20"/>';
    }
    h += '<span style="position: relative; top: -5px;">点击点赞 (' + formList.like_num + ')</span>';
    h += '</td>';
    h += '<td class="td-hd" onclick="commPop();">';
    if (formList.comm_uid > 0) {
        h += '<img src="/imgs/plun1.png" height="20"/>';
    } else {
        h += '<img src="/imgs/plun0.png" height="20"/>';
    }
    h += '<span style="position: relative; top: -5px;">点击留言 (' + formList.comm_num + ')</span>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //插入广告
    h += '<div id="advert">';
    h += '<img src="' + domain + '/videos/2024/20240202.png"/>';
    h += '</div>';
    //储物内容
    var thingList = formList.thingList, thingImgList, ii;
    if (thingList.length > 0) {
        h += '<table>';
        h += '<tr><td colspan="2" class="boxTitle">储物内容</td></tr>';
        for (i = 0; i < thingList.length; i ++) {
            thingImgList = thingList[i].imgList;
            h += '<tr>';
            h += '<td class="tl">储物名称</td>';
            h += '<td class="tr">';
            h += thingList[i].title;
            h += '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tl">储物类型</td>';
            h += '<td class="tr">';
            h += thingList[i].cate_name;
            h += '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tl" style="vertical-align: top;">储物直观图</td>';
            h += '<td class="tr">';
            if (thingImgList.length > 0) {
                for (ii = 0; ii < thingImgList.length; ii ++) {
                    h += '<img src="' + thingImgList[ii].link + '" style="width: 80%;"/>';
                }
            } else {
                h += '-';
            }
            h += '</td>';
            h += '</tr>';
        }
        h += '<td colspan="2">&nbsp;</td>';
        h += '</table>';
    }
    //流量动态
    h += '<table>';
    h += '<tr><td colspan="2" class="boxTitle">互动情况</td></tr>';
    h += '<tr>';
    h += '<td class="tl">访问次数</td>';
    h += '<td class="tr">';
    h += formList.visit_num;
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">点赞总数</td>';
    h += '<td class="tr">';
    h += formList.like_total;
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">评论总数</td>';
    h += '<td class="tr">';
    h += formList.comm_total;
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tl">上次访问</td>';
    h += '<td class="tr">';
    h += formList.lasttime;
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //最近点赞
    h += '<table>';
    h += '<tr><td colspan="2" class="boxTitle">最近点赞</td></tr>';
    //h += '<tr>';
    //h += '<td class="tl">网名</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    //h += '<td class="tl">点赞时间</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    //h += '</tr>';
    //h += '<td colspan="2">&nbsp;</td>';
    //h += '</tr>';
    var zanlist = formList.like_list;
    for (i = 0; i < zanlist.length; i ++) {
        h += '<tr>';
        h += '<td class="tl">用户</td>';
        h += '<td class="tr">';
        h += zanlist[i].uname;
        h += '</td>';
        h += '</tr>';
        h += '<td class="tl">点赞时间</td>';
        h += '<td class="tr">';
        h += zanlist[i].addtime;
        h += '</td>';
        h += '</tr>';
        h += '</tr>';
        h += '<td colspan="2">&nbsp;</td>';
        h += '</tr>';
    }
    h += '</table>';
    //最近留言
    h += '<table>';
    h += '<tr><td colspan="2" class="boxTitle">最近留言</td></tr>';
    //h += '<tr>';
    //h += '<td class="tl">网名</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    //h += '<tr>';
    //h += '<td class="tl">留言内容</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    //h += '<td class="tl">留言时间</td>';
    //h += '<td class="tr">';
    //h += '-';
    //h += '</td>';
    //h += '</tr>';
    //h += '</tr>';
    //h += '<td colspan="2">&nbsp;</td>';
    //h += '</tr>';
    var clist = formList.comm_list;
    for (i = 0; i < clist.length; i ++) {
        h += '<tr>';
        h += '<td class="tl">用户</td>';
        h += '<td class="tr">';
        h += clist[i].uname;
        h += '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tl">留言内容</td>';
        h += '<td class="tr">';
        h += clist[i].detail;
        h += '</td>';
        h += '</tr>';
        h += '<td class="tl">留言时间</td>';
        h += '<td class="tr">';
        h += clist[i].addtime;
        h += '</td>';
        h += '</tr>';
        h += '</tr>';
        h += '<td colspan="2">&nbsp;</td>';
        h += '</tr>';
    }
    h += '</table>';
    $('#form').html(h);
}


//查看箱体大图
function thumbShow(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '', i;
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.2; position: fixed; top: 35px; left: 0; z-index: 10000;"></div>';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch - 35) + 'px; background: #fff; border-top-left-radius: 5px; border-top-right-radius: 5px; position: fixed; top: 100px; left: 0; z-index: 10000;">';
    h += '<a href="javascript:;" style="margin: 10px; color: red; float: right;" onclick="$(\'#bigImgPop\').html(\'\');">关闭</a>';
    for (i = 0; i < formList.thumbList.length; i ++) {
        h += '<img src="' + formList.thumbList[i].link + '" style="margin: 10px; width: ' + (cw - 20) + 'px;"/>';
    }
    h += '</div>';
    h += '</div>';
    $('#bigImgPop').html(h);
}


//点赞
function addLike(){
    var userInfo = getUserInfo();
    console.log(userInfo, formList);
    if (!userInfo || !userInfo.token) {
        //msgPop({msg: '没有登录'});
        loginForm();
        return
    }
    var formData = {
        token: userInfo.token,
        id: formList.id,
    };
    $.ajax({
        type: apis.tmp2LikeAdd.method,
        url: apis.tmp2LikeAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.href = '';
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//评论弹窗
function commPop(){
    var token = getParamByUrl('token');
    if (!token) {
        loginForm();
        return
    }
    var h = '';
    h += '<style type="text/css">#formMask { width: 100%; height: 100vh; background: #000; opacity: 0.2; position: fixed; top: 0; } #formOut { margin: 10px 10%; padding: 10px 5%; width: 70%; background: #fff; border: 1px solid #ccc; border-radius: 10px; box-shadow: 0 0 10px #ccc; position: fixed; top: 20%; } #formOut .group { margin: 20px 0; } #formOut textarea { padding: 10px 5%; width: 90%; height: 100px; border: 1px solid #0088cc; border-radius: 5px; font-size: 16px; } #formOut button { margin: 0 1%; padding: 10px 0; width: 48%; background: #0088CC; color: #fff; border: 0; border-radius: 5px; }</style>';
    h += '<div id="formMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div id="formOut">';
    h += '<div class="group">';
    h += '<textarea type="text" placeholder="说点儿什么" id="cnt"></textarea>';
    h += '</div>';
    h += '<div class="group">';
    h += '<button type="button" style="background: darkorange;" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button type="button" onclick="addComm();">确定</button>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//添加评论
function addComm(){
    //var userInfo = getUserInfo();
    //if (!userInfo || !userInfo.token) {
        //msgPop({msg: '没有登录'});
    //    loginForm();
    //    return
    //}
    var token = getParamByUrl('token');
    if (!token) {
        loginForm();
        return
    }
    var formData = {
        token: userInfo.token,
        id: formList.id,
        cnt: $('#cnt').val()
    };
    if (!formData.cnt) {
        msgPop({msg: '评论内容必须的'});
        return
    }
    console.log(formData);
    $.ajax({
        type: apis.tmp2CommAdd.method,
        url: apis.tmp2CommAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '评论成功'});
            setTimeout(function(){
                top.location.href = '';
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}